<?php
final class Restaurant {
	private $restaurant_id;
	private $name;
	private $url;
	private $ssl;

	private $config;
	
	private $system_config;

	public function __construct($registry, $config) {
		$this->system_config = $config;
		
		$this->db = $registry->get('db');
		$this->request = $registry->get('request');
		$this->session = $registry->get('session');
		$this->user = $registry->get('user');

		$this->config();
		
		if (isset($this->session->data['current_restaurant_id'])) {
			$this->queryData();
		}
	}
	
	private function queryData(){
		$restaurant_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "restaurant s LEFT JOIN " . DB_PREFIX . "restaurant_description sd ON s.restaurant_id = sd.restaurant_id WHERE s.restaurant_id = '" . (int)$this->session->data['current_restaurant_id'] . "' AND sd.language_id = '" . (int)$this->system_config->get('config_language_id') . "'");
					
		if ($restaurant_query->num_rows) {
			$this->restaurant_id = $restaurant_query->row['restaurant_id'];
			$this->name = $restaurant_query->row['name'];
			$this->url = $restaurant_query->row['url'];
			$this->ssl = $restaurant_query->row['ssl'];
		}
	}
	 
	private function config(){
		$this->config = new Config();
			
		// Current Restaurant Settings
		if(isset($this->session->data['current_restaurant_id'])){
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "setting WHERE restaurant_id = '" . $this->session->data['current_restaurant_id'] . "'");

			foreach ($query->rows as $setting) {
				if (!$setting['serialized']) {
					$this->config->set($setting['key'], $setting['value']);
				} else {
					$this->config->set($setting['key'], unserialize($setting['value']));
				}
			}
		}
	}

	public function changeRestaurant() {
		// Only allow for restaurant staff
		if(!$this->user->isRestaurantStaff()){
			unset($this->session->data['current_restaurant_id']);
			$this->restaurant_id = null;
			$this->name = null;
			$this->url = null;
			$this->ssl = null;
			
			return false;
		}
		
		// Check if selected restaurant is belong to current user or not
		// Load all restaurants of current user
		$sql = "SELECT s.* FROM " . DB_PREFIX . "restaurant s" .
			   " INNER JOIN " . DB_PREFIX . "user_to_restaurant us" .
			   " ON s.restaurant_id = us.restaurant_id" .
			   " WHERE s.restaurant_id != '0' AND user_id = '" . (int)$this->user->getId() . "' ORDER BY url";

		$restaurants_query = $this->db->query($sql);

		$found_restaurant_id = false;
		foreach ($restaurants_query->rows as $restaurant) {
			if($restaurant['restaurant_id'] == $this->request->get['current_restaurant_id']){
				$found_restaurant_id = true;
				break;
			}
		}

		if(!$found_restaurant_id){
			unset($this->session->data['current_restaurant_id']);
			$this->restaurant_id = null;
			$this->name = null;
			$this->url = null;
			$this->ssl = null;
			
			return false;
		} else {
			$this->session->data['current_restaurant_id'] = $this->request->get['current_restaurant_id'];

			$this->queryData();

			$this->config();
			return true;
		}
	}

	public function getRestaurantId() {
		return $this->restaurant_id;
	}

	public function getName() {
		return $this->name;
	}
	 
	public function getUrl() {
		return $this->url;
	}
	 
	public function getSSL() {
		return $this->ssl;
	}
	 
	public function getConfig(){
		return $this->config;
	}
}
?>